package com.xusheng.cli.controller.back;

import cn.hutool.core.util.StrUtil;
import com.xusheng.cli.common.annotation.OperationLogAnno;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.enums.OperationType;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.frontUserModule.FrontUser;
import com.xusheng.cli.entity.frontUserModule.VipConfig;
import com.xusheng.cli.entity.vipRecordModule.VipRecord;
import com.xusheng.cli.req.VipRecordQueryReq;
import com.xusheng.cli.service.VipRecordService;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * 会员购买记录接口
 *
 * @author lyh
 * @since 2023-05-06 13:18:27
 */
@Validated
@RestController
@RequestMapping("/api/v1/vipRecord")
public class VipRecordController {
    /**
     * 服务对象
     */
    @Resource
    private VipRecordService vipRecordService;
     
     /**
     * 查询所有数据
     *
     * @param req 查询实体
     * @return 所有数据
     * @author lyh
     * @since 2023-05-06 13:18:27
     */
    @GetMapping("queryAll")
    public Result<List<VipRecord>> queryAll(VipRecordQueryReq req) {
        LocalDateTime start = null, end = null;
        if (req.getStartTime() != null) {
            start = LocalDateTime.of(req.getStartTime(), LocalTime.of(0, 0, 0));
        }
        if (req.getEndTime() != null) {
            end = LocalDateTime.of(req.getEndTime(), LocalTime.of(23, 59, 59));
        }
        JoinLambdaWrapper<VipRecord> wrapper = new JoinLambdaWrapper<>(VipRecord.class);
        wrapper.orderByDesc(VipRecord::getCreateTime)
                .gt(start != null, VipRecord::getCreateTime, start)
                .lt(end != null, VipRecord::getCreateTime, end)
                .eq(req.getVipId() != null, VipRecord::getVipId, req.getVipId())
                .leftJoin(FrontUser.class, FrontUser::getId, VipRecord::getUserId)
                .like(StrUtil.isNotBlank(req.getUserName()), FrontUser::getAccount, req.getUserName())
                .selectAs(FrontUser::getAccount, "userName")
                .end()
                .leftJoin(VipConfig.class, VipConfig::getId, VipRecord::getVipId)
                .selectAs(VipConfig::getName, "vipName")
                .end();
        return Result.success(vipRecordService.joinList(wrapper, VipRecord.class));
    }
    /**
     * 分页查询所有数据
     *
     * @param req 查询条件
     * @return 所有数据
     * @author lyh
     * @since 2023-05-06 13:18:27
     */
    @GetMapping("queryByPage")
    public Result<MyPage<VipRecord>> queryByPage(VipRecordQueryReq req) {
        return Result.success(vipRecordService.recordPage(req));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     * @author lyh
     * @since 2023-05-06 13:18:27
     */
    @GetMapping("queryOne/{id}")
    public Result<VipRecord> selectOne(@PathVariable Serializable id) {
        return Result.success(this.vipRecordService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param vipRecord 实体对象
     * @return 新增结果
     * @author lyh
     * @since 2023-05-06 13:18:27
     */
    @PostMapping("insert")
    @OperationLogAnno(module = "会员购买记录 模块", type = OperationType.CREATE, desc = "添加一条会员购买记录 数据")
    public Result<Object> insert(@Validated(Save.class) @RequestBody VipRecord vipRecord) {
        return Result.isSuccess(vipRecordService.save(vipRecord));
    }

    /**
     * 修改数据
     *
     * @param vipRecord 实体对象
     * @return 修改结果
     * @author lyh
     * @since 2023-05-06 13:18:27
     */
    @PutMapping("update")
    @OperationLogAnno(module = "会员购买记录 模块", type = OperationType.UPDATE, desc = "修改一条会员购买记录 数据")
    public Result<Object> update(@Validated(Update.class) @RequestBody VipRecord vipRecord) {
        return Result.isSuccess(vipRecordService.updateById(vipRecord));
    }
    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     * @author lyh
     * @since 2023-05-06 13:18:27
     */
    @DeleteMapping("delete/{id}")
    @OperationLogAnno(module = "会员购买记录 模块", type = OperationType.UPDATE, desc = "删除一条会员购买记录 数据")
    public Result<Object> delete(@PathVariable Long id) {
        VipRecord vipRecord = new VipRecord();
        vipRecord.setId(id);
		return Result.isSuccess(vipRecordService.removeById(vipRecord));
    }
    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     * @author lyh
     * @since 2023-05-06 13:18:27
     */
    @DeleteMapping("batchDelete")
    @OperationLogAnno(module = "会员购买记录 模块", type = OperationType.UPDATE, desc = "批量删除会员购买记录 数据")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
		return Result.isSuccess(vipRecordService.batchDelete(idList, new VipRecord()));
    }
}

